<template>
  <div>
    <el-form :inline="true" :model="spuQueryForm" ref="searchRef">
      <el-form-item label="分类" prop="categoryId">
        <el-cascader v-model="spuQueryForm.categoryId" :options="allCategoryList" @focus="getCategoryList"
                     :props="cascaderProps"/>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="spuQueryForm.status" class="m-2" placeholder="Select">
          <el-option
              label="已上架"
              value="0"
          />
          <el-option
              label="未上架"
              value="1"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="keyword">
        <el-input v-model="spuQueryForm.keyword" placeholder="请输入关键词"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getSpuList">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchRef.resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="spuPage.list" style="width: 100%" v-loading="loading">
      <el-table-column prop="spuId" label="编号"/>
      <el-table-column prop="spuName" label="名称"/>
      <el-table-column prop="spuDescription" label="描述"/>
      <el-table-column prop="categoryId" label="分类"/>
      <el-table-column prop="brandName" label="品牌"/>
      <el-table-column prop="userId" label="所属用户"/>
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.status==0">正常</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status==1">删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column prop="updateTime" label="更新时间"/>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="danger" size="small" @click="openModify(scope.row.spuId)">修改
          </el-button>
          <el-divider direction="vertical"/>
          <el-button size="small" type="primary">规格</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
          v-model:current-page="spuQueryForm.pageNum"
          v-model:page-size="spuQueryForm.pageSize"
          :page-sizes="[5,8,10]"
          background
          layout="sizes, prev, pager, next"
          :total="spuPage.total"
      />
    </div>
    <el-dialog title="修改商品" v-model="dialogVisible" width="500px">
      <el-form :model="spuModifyForm">
        <el-form-item label="商品名称">
          <el-input v-model="spuModifyForm.spuName"/>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input v-model="spuModifyForm.spuDescription"/>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-cascader v-model="spuModifyForm.categoryId" @focus="getCategoryList" :options="allCategoryList"
                       :props="cascaderProps"/>
        </el-form-item>
        <el-form-item label="品牌">
          <el-input v-model="spuModifyForm.brandName"/>
        </el-form-item>
        <el-form-item label="商品状态">
          <el-radio-group v-model="spuModifyForm.status">
            <el-radio label="0">上架</el-radio>
            <el-radio label="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModify">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {onMounted, reactive, ref, watch} from "vue";
import {reqGetSpuPage, reqGetSpuCategoryTreeList, reqGetSpuById, reqModifySpu} from '@/api/serve/re-market'

let spuPage = reactive({
  list: [],
  pageNum: 1,
  total: 0
})
let loading = ref(true)
let allCategoryList = ref([])
let searchRef = ref()
let spuQueryForm = reactive({
  pageNum: 1,
  pageSize: 5,
  categoryId: '',
  keyword: '',
  status: ''
})
let spuModifyForm = ref({})
let dialogVisible = ref(false)
const cascaderProps = {
  value: 'categoryId',
  label: 'name',
  children: 'children',
  emitPath: false
}

function getCategoryList() {
  reqGetSpuCategoryTreeList().then(res => {
    allCategoryList.value = res.data
  })
}

let openModify = (id) => {
  reqGetSpuById(id).then(res => {
    spuModifyForm.value = res.data
  }).finally(() => {
    dialogVisible.value = true
  })
}

async function getSpuList() {
  let res = await reqGetSpuPage(spuQueryForm)
  Object.assign(spuPage, res.data)
  loading.value = false
}

let submitModify = () => {
  dialogVisible.value = false
  reqModifySpu(spuModifyForm.value)
}

onMounted(() => {
  getSpuList()
})
watch([() => spuQueryForm.pageNum, () => spuQueryForm.pageSize], () => {
  getSpuList()
})
</script>
<style scoped>

</style>
